package com.cordershy.service;

import com.cordershy.mapper.payMapper;
import com.cordershy.pojo.pay;
import com.mysql.cj.xdevapi.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class payService {
    @Autowired
    payMapper payMapper;
    @Transactional(propagation = Propagation.REQUIRED)
    @Cacheable(value = "accountCache",key = "'pay'+#paymonth+#payyear")
    public List<pay> selectByMonth(String paymonth,String payyear,String username){
        List<pay> pays = payMapper.selectByDate(paymonth, payyear,username);
        Collections.reverse(pays);
        Collections.sort(pays);
        return pays;
    }
    @Transactional
    @Cacheable(value = "accountCache",key = "'payid'+#payid")
    public pay selectById(int payid){
        return payMapper.select(payid);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @CachePut(value = "accountCache",key = "'payid'+#pay.payid")
    public pay addPay(pay pay){
        payMapper.insert(pay);
        return pay;
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @CachePut(value = "accountCache",key = "'payid'+#pay.payid")
    public pay updatePay(pay pay){
        pay temp = selectById(pay.getPayid());
        if (temp!=null){
            payMapper.update(pay);
        }else{
            return null;
        }
        return pay;
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @CacheEvict(value = "accountCache",key = "'payid'+#payid")
    public int deletePay(int payid){
        return payMapper.delete(payid);
    }
}
